﻿using NoteSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Policy;
using System.Security.Principal;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NoteSystem
{
    public partial class Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UpdateReadCount();
        }
        #region 更新阅读次数
        public void UpdateReadCount()
        {
            int noteId = Convert.ToInt32(Request["id"]);
            DbHelper.Query("update notes set read_count=read_count+1 where id=" + noteId);
        }
        #endregion

        #region 日志信息绑定
        public string InitNote()
        {
            //1.获取querystring传值  日志id
            int noteId = Convert.ToInt32(Request["id"]);
            //2.根据日志id去数据库查询日志信息
            DataSet ds = DbHelper.Query("select * from notes where id=" + noteId);
            if (ds.Tables[0].Rows.Count == 0)
                return "";
            DataRow noteRow = (DataRow)ds.Tables[0].Rows[0];
            //3.进行替换
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(" <div class=\"show\">");
            stringBuilder.Append($"<h2>{noteRow["title"]}</h2>");
            stringBuilder.Append($"<div class=\"show_time\">发布时间：{noteRow["create_time"]} 阅读：{noteRow["read_count"]}</div>");
            stringBuilder.Append("<p>");
            stringBuilder.Append($"{noteRow["detail"]}");
            stringBuilder.Append("</p>");
            stringBuilder.Append("  </div>");
            return stringBuilder.ToString();
        }
        #endregion

        #region 评论信息加载
        public string InitComment()
        {
            int noteId = Convert.ToInt32(Request["id"]);
            int publicUserId = 0;//发布人id
            int loginUserId = Convert.ToInt32(((DataRow)Session["User"])["id"]);//登录人
            //梳理身份
            //1.日志发布人  评论删除显示  回复删除显示
            DataSet dsNote = DbHelper.Query("select * from notes where id=" + noteId);
            if (dsNote.Tables[0].Rows.Count > 0)
            {
                publicUserId = Convert.ToInt32(dsNote.Tables[0].Rows[0]["user_id"]);
            }
            //2.评论人      评论删除显示
            //3.回复人      回复删除显示
            //4.什么都不是  都不显示

            //1.根据用户点击的日志id查询他的评论
            DataSet ds = DbHelper.Query("select c.*,u.img_url,u.account from comments c inner join users u on u.id=c.user_id where c.note_id=" + noteId);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                stringBuilder.Append("<div class=\"pinglun\">");
                stringBuilder.Append("            <div class=\"pl_left left\">");
                stringBuilder.Append($"      <img src=\"{ds.Tables[0].Rows[i]["img_url"]}\">");
                stringBuilder.Append("        </div>");
                stringBuilder.Append("            <div class=\"pl_right left\">");
                stringBuilder.Append($"       <p class=\"p_a\"><span>{i + 1}楼</span> {ds.Tables[0].Rows[i]["account"]} 评论时间: {ds.Tables[0].Rows[i]["create_time"]}</p>");
                stringBuilder.Append($"   <p>{ds.Tables[0].Rows[i]["detail"]} <a onclick=\"alertReply({ds.Tables[0].Rows[i]["id"]})\">回复</a>&nbsp;&nbsp;");
                int commentUserId = Convert.ToInt32(ds.Tables[0].Rows[i]["user_id"]);//当前遍历行 评论人
                if (loginUserId == publicUserId || loginUserId == commentUserId)
                {
                    stringBuilder.Append($"<a onclick='delComment({ds.Tables[0].Rows[i]["id"]})'>删除评论</a>");
                }
                stringBuilder.Append("</p>");
                stringBuilder.Append(" <div class=\"reply\">");
                stringBuilder.Append("<ul id=\"ul1\">");
                DataSet dsReply = DbHelper.Query("select r.*,u.account,u.img_url from replys r inner join users u on u.id=r.user_id where comment_id=" + ds.Tables[0].Rows[i]["id"]);
                if (dsReply.Tables[0].Rows.Count > 0)
                {
                    for (int j = 0; j < dsReply.Tables[0].Rows.Count; j++)
                    {
                        stringBuilder.Append(" <li>");
                        stringBuilder.Append($" <img src=\"{dsReply.Tables[0].Rows[j]["img_url"]}\" width=\"35\" class=\"left\">");
                        stringBuilder.Append(" <p class=\"left\">");
                        stringBuilder.Append($" <span>{dsReply.Tables[0].Rows[j]["account"]}&nbsp;</span>{dsReply.Tables[0].Rows[j]["detail"]}<br>");
                        stringBuilder.Append($" {dsReply.Tables[0].Rows[j]["create_time"]}");
                        int replyUserId = Convert.ToInt32(dsReply.Tables[0].Rows[j]["user_id"]);//回复人
                        if (loginUserId == publicUserId || loginUserId == replyUserId)
                        {
                            stringBuilder.Append($"<a onclick='delReply({dsReply.Tables[0].Rows[j]["id"]})' href=\"#\"><span class=\"glyphicon glyphicon-trash\"></span>删除</a>");
                        }
                        stringBuilder.Append("    </p>");
                        stringBuilder.Append("  </li>");
                    }
                }
                stringBuilder.Append(" </ul>");
                stringBuilder.Append("   </div>");
                stringBuilder.Append(" </div>");
            }
            return stringBuilder.ToString();
        }
        #endregion

        #region  评论发布
        protected void btnComment_Click(object sender, EventArgs e)
        {
            // int userId=从session里拿
            DataRow userRow = (DataRow)Session["User"];
            int user_id = Convert.ToInt32(userRow["id"]);
            int noteId = Convert.ToInt32(Request["id"]);
            string detail = txtComment.Value;
            //插入insert
            string sql = string.Format(@"insert into comments values({0},{1},'{2}','{3}')", user_id, noteId,detail, DateTime.Now);
            //执行更新操作
            int flag = DbHelper.ExecuteSql(sql);
            if (flag > 0)
            {
                Response.Write("<script>alert('评论成功')</script>");
            }
            //1)InitComment();
            //2)重新定向到当前页面内
            Response.Redirect("Detail.aspx?id=" + noteId); //等价于重新加载了一遍页面 
        }
        #endregion
    }
}